#include <bits/stdc++.h>
using namespace std;

class Solution
{
public:
    using Matrix = vector<vector<char>>;

    int main()
    {
        int n;
        cin >> n;
        Matrix origin = readMatrix(n);
        Matrix target = readMatrix(n);
        Matrix mat = origin;
        turn90Clockwise(mat);
        if (isEqualMatrix(mat, target))
        {
            cout << 1;
            return 0;
        }
        turn90Clockwise(mat);
        if (isEqualMatrix(mat, target))
        {
            cout << 2;
            return 0;
        }
        turn90Clockwise(mat);
        if (isEqualMatrix(mat, target))
        {
            cout << 3;
            return 0;
        }
        mat = origin;
        horizontalReverse(mat);
        if (isEqualMatrix(mat, target))
        {
            cout << 4;
            return 0;
        }
        turn90Clockwise(mat);
        if (isEqualMatrix(mat, target))
        {
            cout << 5;
            return 0;
        }
        turn90Clockwise(mat);
        if (isEqualMatrix(mat, target))
        {
            cout << 5;
            return 0;
        }
        turn90Clockwise(mat);
        if (isEqualMatrix(mat, target))
        {
            cout << 5;
            return 0;
        }
        if (isEqualMatrix(origin, target))
        {
            cout << 6;
            return 0;
        }
        cout << 7;
        return 0;
    }

    Matrix readMatrix(int n)
    {
        Matrix mat(n, vector<char>(n));
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                cin >> mat[i][j];
            }
        }
        return move(mat);
    }

    void turn90Clockwise(Matrix &mat)
    {
        int n = mat.size();
        for (int i = 0; i < n / 2; ++i)
        {
            for (int j = 0; j < (n + 1) / 2; ++j)
            {
                char temp = mat[i][j];
                mat[i][j] = mat[n - 1 - j][i];
                mat[n - 1 - j][i] = mat[n - 1 - i][n - 1 - j];
                mat[n - 1 - i][n - 1 - j] = mat[j][n - 1 - i];
                mat[j][n - 1 - i] = temp;
            }
        }
    }

    void horizontalReverse(Matrix &mat)
    {
        int n = mat.size();
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < n / 2; ++j)
            {
                swap(mat[i][j], mat[i][n - 1 - j]);
            }
        }
    }

    bool isEqualMatrix(Matrix &mat1, Matrix &mat2)
    {
        int n = mat1.size();
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                if (mat1[i][j] != mat2[i][j])
                {
                    return false;
                }
            }
        }
        return true;
    }
};

#ifndef __LOCAL_TEST__
int main()
{
    return Solution().main();
}
#endif